package org.example.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.example.exception.AppException;
import org.example.exception.AppExceptionCodeMsg;
import org.example.pojo.Disease;
import org.example.pojo.MedicalRecordDisease;
import org.example.service.IMedicalRecordDiseaseService;
import org.example.service.IMedicalRecordService;
import org.example.vo.ResultBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author peak
 * @since 2024-12-18
 */
@RestController
@RequestMapping("/recorddisease")
public class MedicalRecordDiseaseController {
    private static final Logger log = LoggerFactory.getLogger(MedicalRecordDiseaseController.class);
    @Autowired
    private IMedicalRecordDiseaseService service;
    @GetMapping("/mrid/{mrid}")
    public ResultBean getByMrId(@PathVariable("mrid") Integer mrid) {
        MPJLambdaWrapper<MedicalRecordDisease> wrapper = new MPJLambdaWrapper<>();
        wrapper.setAlias("mrd")
                .selectAll(MedicalRecordDisease.class,"mrd")
                .selectAs(Disease::getDiseaseName,"'disease.diseaseName'")
                .selectAs(Disease::getDiseaseCode,"'disease.diseaseCode'")
                .leftJoin(Disease.class,"d",Disease::getId,MedicalRecordDisease::getDiseaseId)
                .eq("mrd.medical_record_id",mrid);

        List<MedicalRecordDisease> list = service.list(wrapper);
        return ResultBean.SUCCESS(list);
    }
    //批量添加病例，就是往只有两个id 表进行添加
    @PostMapping("/addbatch")
    public ResultBean addbatch(@RequestBody List<MedicalRecordDisease> medicalRecordDiseases) {
        if(medicalRecordDiseases==null || medicalRecordDiseases.size()==0) {
            throw new AppException(AppExceptionCodeMsg.PARAMS_EMPTY);
        }
        Boolean b=service.saveBatch(medicalRecordDiseases);
        return ResultBean.SUCCESS();
    }
    @GetMapping("/remove/{medicalRecordId}/{diseaseId}")
    public ResultBean removeById(@PathVariable("medicalRecordId")Integer id1,@PathVariable("diseaseId")Integer id2) {
       UpdateWrapper<MedicalRecordDisease> wrapper = new UpdateWrapper<>();
       wrapper.eq("medical_record_id",id1)
               .eq("disease_id",id2);
        boolean removed = service.remove(wrapper);
        return removed?ResultBean.SUCCESS():ResultBean.ERROR("删除失败");
    }
}
